Skip to content

add non-jsx view to interactive code samples #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 16, 2017

Conversation

criticalbh
Copy link
Contributor

#99

Hello @bvaughn .

Here is implementation of code interaction view:
image

and once clicked on VIEW COMPILED JSX we get this:
image

@facebook-github-bot
Copy link
Collaborator

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot
Copy link
Collaborator

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@reactjs-bot
Copy link

reactjs-bot commented Oct 11, 2017

Deploy preview ready!

Built with commit 8a0eda3

https://deploy-preview-141--reactjs.netlify.com

@criticalbh criticalbh changed the title add non-jsx view to to interactive code samples add non-jsx view to interactive code samples Oct 11, 2017
Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some feedback.

Here's a rough prototype I made (demonstrating the feedback I left in the PR) if you're interested:

https://gist.github.com/bvaughn/c65d556693fed3dbc6ab2025efac2c2b

@@ -11,13 +11,19 @@ import ReactDOM from 'react-dom';
import Remarkable from 'remarkable';
// TODO: switch back to the upstream version of react-live
// once https://github.com/FormidableLabs/react-live/issues/37 is fixed.
import {LiveProvider, LiveEditor} from '@gaearon/react-live';
import {LiveEditor, LiveProvider} from '@gaearon/react-live';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆 Love the ninja alpha-sort


const compile = code =>
Babel.transform(code, {presets: ['es2015', 'react']}).code; // eslint-disable-line no-undef

const compileJsxToJS = code => Babel.transform(code, {presets: ['react']}).code; // eslint-disable-line no-undef
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about these names for improved clarity?

// eslint-disable-next-line no-undef
const compileES5 = code =>
  Babel.transform(code, {presets: ['es2015', 'react']}).code;

// eslint-disable-next-line no-undef
const compileES6 = code => Babel.transform(code, {presets: ['react']}).code;

spellCheck="false"
dangerouslySetInnerHTML={{__html: prism(compiledJsx)}}
/>
)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's re-use the LiveProvider+LiveEditor to show the non-JSX code too. This way we don't have to use Prism directly. So something like:

<LiveProvider code={showJSX ? code : compiledES6} mountStylesheet={false}>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, compiledES6 would be editable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point. I guess this wouldn't quite work as proposed. Maybe we should make the ES6 preview not editable?

Back to JSX Editor
</a>
)}
</MetaTitle>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about using a checkbox to toggle JSX mode instead of a button? I think it might look better 😄

screen shot 2017-10-12 at 10 23 20 am

@criticalbh
Copy link
Contributor Author

@bvaughn Thank you very much, I'll jump on it.

@criticalbh
Copy link
Contributor Author

@bvaughn I have made both ES5 and ES6 editable.

There might be small problem when coming back to JSX view since what we have changed in JS wont be applied to JSX.

If this scenario is not acceptable than we can make JSX compiled view to be preview only.

Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be small problem when coming back to JSX view since what we have changed in JS wont be applied to JSX.

If this scenario is not acceptable than we can make JSX compiled view to be preview only.

That's a good point!

I think it's probably okay. I don't really think that's a common use case or important enough to warrant added complexity.

@@ -282,7 +312,7 @@ class CodeEditor extends Component {
}

_onChange = code => {
this.setState(this._updateState(code));
this.setState(this._updateState(code, this.state.showJSX));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change this to:

this.setState(state => this._updateState(code, state.showJSX));

The benefit of using a callback for setState is that it avoids references potentially stale values in this.state. This didn't matter before because we weren't references this.state but now that we are....we should use the callback form. 😄

return {
compiled: compile(code),
let newState = {
compiled: compileES5(code),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should code always go into newState? What's the benefit of only adding it conditionally.

Copy link
Contributor Author

@criticalbh criticalbh Oct 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I thought here is that we want to keep code only if we are in JSX view.

If we mix code from ES5 view with ES6 it wont work any more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see what you mean.

Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 1-way edit flow for JSX-to-non-JSX bugs me a bit but I think it's probably not an actual problem and I can't think of a reasonable solution. 😄

Thanks for all of your work on this!

return {
compiled: compile(code),
let newState = {
compiled: compileES5(code),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see what you mean.

@bvaughn bvaughn merged commit 361e27c into reactjs:master Oct 16, 2017
@criticalbh
Copy link
Contributor Author

Yes indeed, it is cost for ES5 being editable. Thank you very much for guidance it was awesome.

@criticalbh
Copy link
Contributor Author

Hello @bvaughn, what are the reasons of this PR being overwritten with 2b44cd3 ?

@bvaughn
Copy link
Contributor

bvaughn commented Oct 17, 2017

It wasn't overridden? I just tweaked the wording slightly when testing the changes to the home template. (Without those changes, a blocked Babel script just caused the home page to show "Loading examples" forever.)

@criticalbh
Copy link
Contributor Author

Yes I saw the reason, also sometimes after the page is started and you scroll down, new refresh happens with editors being in the place.

But regarding is it overridden, it is. Here is raw file on master branch for CodeEditor.js https://raw.githubusercontent.com/reactjs/reactjs.org/master/src/components/CodeEditor/CodeEditor.js and there are no ES5 transpilers.

@bvaughn
Copy link
Contributor

bvaughn commented Oct 17, 2017

Oh whoa! I see what you mean! Looks like something went wrong with the merge. Thank you for pointing that out! I'll fix.

@criticalbh
Copy link
Contributor Author

@bvaughn your welcome, no problems.

@bvaughn
Copy link
Contributor

bvaughn commented Oct 17, 2017

😅 too much context-switching this morning.

Very sorry this happened. I'll get it fixed right away.

@criticalbh
Copy link
Contributor Author

hh I believe, no problems at all, it is good it is noticed on time.

jhonmike pushed a commit to jhonmike/reactjs.org that referenced this pull request Jul 1, 2020
BetterZxx pushed a commit to BetterZxx/react.dev that referenced this pull request Mar 21, 2023
…js#141)

* finished translating para 1 and 2

* finished translating para 3 and 4

* altered wording

* use Chinese character of question mark

* use Chinese character of full stop

* Update content/docs/faq-internals.md

Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com>

* Update content/docs/faq-internals.md

Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com>

* Update content/docs/faq-internals.md

Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com>

* Update content/docs/faq-internals.md

Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com>

* Update faq-internals.md

* Update content/docs/faq-internals.md

Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com>

* Update content/docs/faq-internals.md

Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com>

* Update content/docs/faq-internals.md

Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com>

* Update content/docs/faq-internals.md

Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants